package com.maowu.pooling;

/**
 * ConnectionFactory will be used to create connection, make sure the connection is fine, and destroy connection.
 * @author maowu
 *
 */
public interface ConnectionFactory
{
    /**
     * create a new conection.
     * DO NOT return null. Throw FailedToConnectException if connection failed in connectTimeout time 
     * or other connection problem.
     * 
     * @param connectTimeout connect timeout. Unit is MS.
     * @return return the connection object. 
     * @throws FailedToConnectException Throw FailedToConnectException if connection failed in connectTimeout time 
     * or other connection problem.
     */
    Object createConnection(long connectTimeout)throws FailedToConnectException;
    
    /**
     * make sure the connection is ok.
     * if the connection is connected, then no need to do any thing.
     * if it is not connected or disconnected, then try to connect again.
     * 
     * @param conn  the connection object.
     * @param connectTimeout connect timeout. Unit is MS.
     * @throws FailedToConnectException,Throws FailedToConnectException when failed to connect 
     * with the other end in connectTimeout time or other connection problem.
     * 
     * @throws ConnectionDamagedException throws ConnectionDamagedException when the connection is damaged 
     * and it is not able to fix it, this connection object should be abandon.
     */
    void touchConnection(Object conn, long connectTimeout)throws FailedToConnectException, ConnectionDamagedException;

    /**
     * destroy this connection.
     * @param conn
     */
    void destroyConnection(Object conn);
}
